﻿/*
* Ext Gantt 1.2
* Copyright(c) 2009-2011 Mats Bryntse Consulting
* mats@ext-scheduler.com
* http://www.ext-scheduler.com/license.html
*
*/

Ext.ns('Sch.gantt');

/**
* @class Sch.gantt.ProgressBarResize
* @extends Ext.util.Observable
* Internal plugin enabling resizing of a task progress bar
* @constructor
* @param {Object} config The object containing the configuration of this model.
*/
Sch.gantt.ProgressBarResize = function (gantt) {
    this.gantt = gantt;

    this.gantt.on({
        afterrender: this.onGanttRender,
        destroy: this.cleanUp,
        scope: this
    });
    Sch.gantt.ProgressBarResize.superclass.constructor.call(this);
};

Ext.extend(Sch.gantt.ProgressBarResize, Ext.util.Observable, {
    /**
    * @cfg useTooltip {Boolean} false to not show a tooltip while resizing
    */
    useTooltip: true,

    /**
    * @cfg {Int} increment
    * The increment in percent to use during a progress element resize
    */
    increment: 10,

    // private
    onGanttRender: function () {
        var g = this.gantt;

        g.mon(g.getView().mainBody, 'mousedown', this.onMouseDown, this, { delegate: '.sch-gantt-progressbar-handle' });
    },

    onMouseDown: function (e, t) {
        var g = this.gantt,
            rec = g.resolveTask(t);

        if (g.fireEvent('beforeprogressbarresize', g, rec)) {
            var progBar = Ext.fly(e.getTarget('.sch-gantt-item')).child('.sch-gantt-progress-bar');
            e.stopEvent();
            
            this.createResizable(progBar, rec, e);
            g.fireEvent('progressbarresizestart', g, rec);
        }
    },

    // private
    createResizable: function (el, taskRecord, e) {

        var t = e.getTarget(),
            taskEl = el.up(this.gantt.eventSelector),
            taskWidth = taskEl.getWidth() - (Ext.isBorderBox ? 4 : 2),
            widthIncrement = taskWidth * this.increment / 100;

        var resizable = new Sch.LazyResizable(el, {
            taskRecord: taskRecord,
            taskWidth: taskWidth,
            handles: 'e',
            dynamic: true,
            widthIncrement: widthIncrement,
            minWidth: 0,
            maxWidth: taskWidth,
            listeners: {
                partialresize: { fn: this.partialResize, scope: this },
                resize: { fn: this.afterResize, scope: this }
            }
        },
            'east',
            e
        );

        resizable.proxy.setStyle('border', '0 none');

        if (this.useTooltip) {
            if (!this.tip) {
                this.tip = new Ext.QuickTip({
                    autoHide: false,
                    anchor: 'b',
                    anchorToTarget: true,
                    html: '%'
                });
            }
            this.tip.initTarget(el);

            this.tip.show();
            this.tip.body.update(taskRecord.get('PercentDone') + '%');
        }
    },

    // private
    partialResize: function (r, newWidth) {
        var percent = Math.round(newWidth * 100 / (r.taskWidth * this.increment)) * this.increment;

        if (this.useTooltip) {
            this.tip.body.update(percent + '%');
        }
    },

    // private
    afterResize: function (r, w, h, e) {
        this.gantt.fireEvent('afterprogressbarresize', this.gantt, r.taskRecord);
        
        if (this.useTooltip) {
            this.tip.hide();
        }

        var percent = Math.round(w * 100 / r.taskWidth);

        r.taskRecord.set('PercentDone', percent);

        // Destroy resizable 
        r.destroy();
    },

    cleanUp: function () {
        if (this.tip) {
            this.tip.destroy();
        }
    }
}); 
